<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- basictypes.qdoc -->
  <title>list QML基本类型 | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-qmlmodule.html">QML Types</a></td><td >list QML基本类型</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc"><span lang="zh-cn">目录</span></a></h3>
<ul>
<li class="level1"><a href="#using-the-list-type">使用列表类型</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">list QML基本类型</h1>
<span class="subtitle"></span>
<!-- $$$list-description -->
<div class="descr"> <a name="details"></a>
<p>QML对象列表。</p>
<p>&nbsp;<code>list</code> 类型指的是一个QML对象列表。</p>
<p>列表值可以通过类似JavaScript数组的方式访问:</p>
<ul>
<li>值使用方括号语法赋值，并用逗号分隔</li>
<li><code>length</code> 属性提供列表中项目的数量</li>
<li>使用[<code>index</code>]语法访问列表中的值</li>
</ul>
<p>可以使用push方法将值动态添加到列表中，就像它是一个JavaScript数组一样</p>
<p><code>list</code> 只能存储QML对象，不能包含任何<a href="qtqml-typesystem-basictypes.html">基本类型</a>值。(要在列表中存储基本类型，请使用<a href="qml-var.html">var</a>类型。)</p>
<p>当与C++集成时，请注意任何<a href="qtqml-cppintegration-data.html">从C++传递到QML</a>的<a href="qqmllistproperty.html">QQmlListProperty</a>值都会自动转换为列表值，反之亦然。</p>
<h2 id="using-the-list-type">使用列表类型</h2>
<p>例如，<a href="../qtquick/qml-qtquick-item.html">Item</a>类型有一<a href="../qtquick/qml-qtquick-item.html#states-prop">状态s</a>列表类型属性，可以按如下方式赋值和使用:</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
      <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>

      <span class="name">states</span>: [
          <span class="type"><a href="qml-qtqml-statemachine-state.html">State</a></span> { <span class="name">name</span>: <span class="string">&quot;activated&quot;</span> },
          <span class="type"><a href="qml-qtqml-statemachine-state.html">State</a></span> { <span class="name">name</span>: <span class="string">&quot;deactivated&quot;</span> }
      ]

      <span class="name">Component</span>.onCompleted: {
          <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;Name of first state:&quot;</span>, <span class="name">states</span>[<span class="number">0</span>].<span class="name">name</span>)
          <span class="keyword">for</span> (var i = 0; <span class="name">i</span> <span class="operator">&lt;</span> <span class="name">states</span>.<span class="name">length</span>; i++)
              <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;state&quot;</span>, <span class="name">i</span>, <span class="name">states</span>[<span class="name">i</span>].<span class="name">name</span>)
      }
  }

</pre>
<p>已定义的<a href="qml-qtqml-statemachine-state.html">State</a>对象将按定义的顺序添加到状态列表中。</p>
<p>如果列表只包含一个对象，方括号可以省略:</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
      <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
      <span class="name">states</span>: <span class="name">State</span> { <span class="name">name</span>: <span class="string">&quot;activated&quot;</span> }
  }

</pre>
<p>注意，对象一旦创建，就不能单独添加或从列表中删除;要修改列表的内容，必须将其重新分配给一个新列表。</p>
<p><b>注意: </b>不建议将<code>list</code> 类型作为自定义属性的类型。因此应该使用<code>var</code> 类型，因为由<code>var</code> 
类型存储的列表可以在QML中以更大的灵活性进行操作。</p><p>这个基本类型由QML语言提供。</p>
</div>
<p><b>请参考 </b><a href="qtqml-typesystem-basictypes.html">QML基本类型</a><span lang="zh-cn">。</span></p>
<!-- @@@list -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
